package com.lightsaber.trade.cloth.web.reporting;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.lightsaber.trade.cloth.dao.archive.ArchivedImageDao;
import com.lightsaber.trade.cloth.entity.archive.ArchivedImage;
import com.lightsaber.trade.cloth.entity.category.Category;
import com.lightsaber.trade.cloth.service.category.CategoryService;
import com.lightsaber.trade.cloth.service.param.ParamObject;
import com.lightsaber.trade.cloth.service.param.impl.ParamObjectImpl;
import com.lightsaber.trade.cloth.service.reporting.ReportingParamService;

@Namespace("/reporting")
public class ReportingAction extends ReportingBaseAction {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ArchivedImageDao archivedImageDao;

    @Autowired
    private ReportingParamService reportingParamService;

    private Integer planYear;
    private String campuses;

    private static final long serialVersionUID = 1L;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public void ajax() {
        Category category = new Category();
        category.setPlanYear(planYear);
        List<Category> categories = categoryService.search(category);
        List<ParamObject> planList = new ArrayList<ParamObject>();

        if (null != categories && !categories.isEmpty()) {
            for (Category c : categories) {
                ParamObject e = new ParamObjectImpl(c.getId(), c.getPlanningExerciseName());
                planList.add(e);
            }

            Map<String, List<ParamObject>> resultMap = new HashMap<String, List<ParamObject>>();

            List<ArchivedImage> archives = archivedImageDao.getByPlanningExercise(category);
            List<ParamObject> archivesList = new ArrayList<ParamObject>();

            for (ArchivedImage a : archives) {
                ParamObject e = new ParamObjectImpl(a.getId(), a.getName());
                archivesList.add(e);
            }

            resultMap.put("plans", planList);
            resultMap.put("images", archivesList);

            Struts2Utils.renderJson(resultMap);
        } else {
            Struts2Utils.renderJson("null");
        }

    }

    public void ajaxDept() {
        if (null != campuses && !campuses.equals("")) {
            String[] campusArray = campuses.split("@");
            if (campusArray.length > 0) {
                List<ParamObject> deptList = reportingParamService.getDeptListByCampus(campusArray);
                Struts2Utils.renderJson(deptList);
            } else {
                Struts2Utils.renderJson("null");
            }
        } else {
            Struts2Utils.renderJson("null");
        }
    }

    public Integer getPlanYear() {
        return planYear;
    }

    public void setPlanYear(final Integer planYear) {
        this.planYear = planYear;
    }

    public String getCampuses() {
        return campuses;
    }

    public void setCampuses(final String campuses) {
        this.campuses = campuses;
    }

}
